// ActionScript file

private var appUpdater:ApplicationUpdaterUI = new ApplicationUpdaterUI();
public var SAVED_TEXT:String = "Notes: ";
private var updateXMLURL:String = "http://www.netgfx.com/postgrid/update.xml";

// Pre-read the server-side XML file to look for <force>yes</force>
public function preCheckForUpdate():void{
	var loader:URLLoader = new URLLoader();
	loader.addEventListener(Event.COMPLETE, checkForUpdate);
	loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError); // Just in case we can't read the update.xml						
	
	loader.load(new URLRequest(updateXMLURL));
}

public function checkForUpdate(event:Event):void {
	setApplicationVersion(); // Find the current version so we can show it below
	
	var myXML:XML = new XML(event.target.data);
	namespace items = "http://ns.adobe.com/air/framework/update/description/1.0";
	use namespace items;
	var forceUpdate:String = myXML.force;
	
	appUpdater.updateURL = updateXMLURL; // Server-side XML file describing update
	appUpdater.isCheckForUpdateVisible = false; // We won't ask permission to check for an update
	
	// If <force>yes</force> was found above, turn off the download and install dialogs
	if(forceUpdate.toLowerCase() == "yes") {
		appUpdater.isDownloadUpdateVisible = false;
		appUpdater.isInstallUpdateVisible = false;
	}
	appUpdater.addEventListener(UpdateEvent.INITIALIZED, onUpdate); // Once initialized, run onUpdate
	appUpdater.addEventListener(ErrorEvent.ERROR, onError); // If something goes wrong, run onError
	appUpdater.initialize(); // Initialize the update framework
}

private function onError(event:ErrorEvent):void {
	Alert.show(event.toString());
}

private function onIOError(event:IOErrorEvent):void
{
	trace("Error loading URL.");
}

private function onUpdate(event):void {
	appUpdater.checkNow(); // Go check for an update now
}


// Find the current version for our Label below
private function setApplicationVersion():void {
	var appXML:XML = NativeApplication.nativeApplication.applicationDescriptor;
	var ns:Namespace = appXML.namespace();
	ver.text = "v" + appXML.ns::version;
}